###############################
# Table A6
###############################

rm(list=ls())

# load packages 
library(foreign)
library(lmtest)
library(sandwich)
library(stargazer)
library(msm)
library("readxl")
library(cregg)
library("ggplot2")
library(gridExtra)
library(readstata13)

# load function that does clustered SEs
vcovCluster <- function(
  model,
  cluster
)
{
  require(sandwich)
  require(lmtest)
  if(nrow(model.matrix(model))!=length(cluster)){
    stop("check your data: cluster variable has different N than model")
  }
  M <- length(unique(cluster))
  N <- length(cluster)           
  K <- model$rank   
  if(M<50){
    warning("Fewer than 50 clusters, variances may be unreliable (could try block bootstrap instead).")
  }
  dfc <- (M/(M - 1)) * ((N - 1)/(N - K))
  uj  <- apply(estfun(model), 2, function(x) tapply(x, cluster, sum));
  rcse.cov <- dfc * sandwich(model, meat = crossprod(uj)/N)
  return(rcse.cov)
}

##############################
# Prepare data
##############################

# load data
load("replication_data.Rdata")
names(d)

##################
# Diagnostics
##################

# Random assigment: regress age on attributes (regress covariate on treatment)
diag3 <- lm(d$education_resp ~ d$speed_eng + d$theft_eng + d$gender_eng + d$partyid_eng + d$age_eng + d$education_eng)
diag3_c <- round(coeftest(diag3, vcov = vcovCluster(diag3, cluster = d$idnum)),4)
diag3_c

######################
# Regression tables
######################

# Random assigment
diag3_c
stargazer(diag3_c,title="Balance test: Age",
          type = "html",
          align=TRUE, 
          omit.stat=c("LL","ser","f"), 
          omit = c("Constant"),
          covariate.labels=c("Speed Corruption: Has received bribes",
                             "Theft Corruption: Has diverted public funds",
                             "Gender: Woman",
                             "Partisanship: Radical Auténtico party",
                             "Partisanship: Colorado party",
                             "Age: 40 years old ",
                             "Age: 50 years old ",
                             "Education: Secondary education",
                             "Education: College education"),
          no.space=TRUE,  
          table.placement = "H",
          #single.row = TRUE,
          star.cutoffs = c(0.05,0.01,0.001),
          dep.var.caption  = "Outcome",
          dep.var.labels   = "Age",
          out="tableA6.html")
          
